class ComicController {
	def comicService
	
    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        [comicInstanceList: Comic.list(params), comicInstanceTotal: Comic.count()]
    }

    def create = {
        def comicInstance = new Comic()
        comicInstance.properties = params
        return [comicInstance: comicInstance]
    }

    def save = {
		def result = comicService.add(request.getFile('file'), params, session.user)
		if(!result.instance.hasErrors()){
			flash.message = "comic.created"
            flash.args = [result.instance.id]
            flash.defaultMessage = "Comic ${result.instance.id} created"
            redirect(action: "show", id: result.instance.id)
    	}
		else {
			render(view: "create", model: [comicInstance: result.instance])
		}
    }

    def show = {
        def comicInstance = Comic.get(params.id)
        if (!comicInstance) {
            flash.message = "comic.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Comic not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [comicInstance: comicInstance]
        }
    }

    def edit = {
        def comicInstance = Comic.get(params.id)
        if (!comicInstance) {
            flash.message = "comic.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Comic not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [comicInstance: comicInstance]
        }
    }

	def enable = {
    	params.disabled = false
    	def result = comicService.update(null, params)
    	if(result != null) {
        	if(result.hasErrors()){
        		render(view: "edit", model: [comicInstance: result])
        	}else{
        		flash.message = "comic.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Comic ${params.id} updated"
                redirect(action: "show", id: result.id)
        	}
        }else{
            flash.message = "comic.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Comic not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }
    
    def disable = {
    	params.disabled = true
    	def result = comicService.update(null, params)
    	if(result != null) {
        	if(result.hasErrors()){
        		render(view: "edit", model: [comicInstance: result])
        	}else{
        		flash.message = "comic.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Comic ${params.id} updated"
                redirect(action: "show", id: result.id)
        	}
        }else{
            flash.message = "comic.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Comic not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }
	
    def update = {
        def result = comicService.update(request.getFile('file'), params)
        if(result != null) {
        	if(result.hasErrors()){
        		render(view: "edit", model: [comicInstance: result])
        	}else{
        		flash.message = "comic.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Comic ${params.id} updated"
                redirect(action: "show", id: result.id)
        	}
        }else{
            flash.message = "comic.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Comic not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def comicInstance = Comic.get(params.id)
        if (comicInstance) {
            try {
                comicInstance.delete()
                flash.message = "comic.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Comic ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "comic.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Comic ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "comic.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Comic not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
